package com.bit.bitstudy;

import java.util.*;

import android.content.*;
import android.view.*;
import android.widget.*;

public class ChatMsgAdapter extends BaseAdapter {

	private Context context;
	private LayoutInflater Inflater;
	private ArrayList<ChatMsg> arSrc;
	private int layout;
	
	ChatMsgAdapter(Context context, int layout, 
			ArrayList<ChatMsg> arData){
		this.context = context;
		this.Inflater = (LayoutInflater)context.getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);
		this.arSrc = arData;
		this.layout = layout;
	}
	
	@Override
	public int getCount() {
		return arSrc.size();
	}

	@Override
	public Object getItem(int position) {
		ChatMsg msg = arSrc.get(position);
		if (msg.MyMsg.length() != 0)
			return msg.MyMsg;
		
		return msg.OtherMsg;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = this.Inflater.inflate(layout, parent, false);
		}
		
		TextView otherText = (TextView)convertView.findViewById(
				R.id.othermsg);
		otherText.setText(arSrc.get(position).OtherMsg);
		TextView myText = (TextView)convertView.findViewById(
				R.id.mymsg);
		myText.setText(arSrc.get(position).MyMsg);
		
		return convertView;
	}

}
